package com.xlf.common.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class Order extends Model<Order> {
	public static final Order dao = new Order();
	
	/**
	 * 关联查询OrderCatalog
	 * @return
	 */
	public List<OrderCatalog> getOrderCatalogs() {
		return OrderCatalog.dao.find("select * from ordercatalog where o_id=? order by c_id", get("o_id"));
	}
	
	public void deleteOrderInfo(String oid) {
		Db.update("delete from orderinfo where o_id=?", oid);
		Db.update("delete from ordercatalog where o_id=?", oid);
		Db.update("delete from orderitem where o_id=?", oid);
	}

	public Page<Order> exeSearchSql(int pageNumber, int pageSize, String oid,String uid, String createtime, String designname,int status) {

		StringBuffer where = new StringBuffer(" from orderinfo where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if (oid != null && !"".equals(oid.trim())) {
			where.append(" and o_id like ?");
			param.add("%" + oid + "%");
		}
		
		if	(uid != null && !"".equals(uid.trim())) {
			where.append(" and u_id=?");
			param.add(Integer.parseInt(uid));
		}
		
		if	(createtime != null && !"".equals(createtime.trim())) {
			where.append(" and createTime like ?");
			param.add("%" + createtime + "%");
		}
		if	(designname != null && !"".equals(designname.trim())) {
			where.append(" and designerName like ?");
			param.add("%" + designname + "%");
		}
		if (status != -1) {
			where.append(" and status=?");
			param.add(status);
		}
		where.append(" order by createTime desc ");
		System.out.println(where.toString());
		return paginate(pageNumber, pageSize, "select *", where.toString(), param.toArray());
	}

	public Order exeFindById(Integer id) {
		StringBuffer whee = new StringBuffer(" from orderinfo where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if (id != null && id > 0) {
			whee.append(" and p_id = ?");
			param.add(id);
		}
		return paginate(1, 1, "select *", whee.toString(), param.toArray())
				.getList().get(0);
	}
	
	/**
	 * 厂家查询所管订单信息
	 * @param pageNumber
	 * @param pageSize
	 * @param o_id
	 * @param createtime
	 * @param status
	 * @param f_id
	 * @return
	 */
	public Page<Order> exeOrderSearch(int pageNumber, int pageSize, String o_id, String createtime, String status, int f_id) {
		// SELECT distinct a.o_id, custerName, custerTel FROM orderinfo a LEFT JOIN orderitem b on a.o_id = b.o_id where b.f_id =1
		StringBuffer where = new StringBuffer(" from orderinfo a LEFT JOIN orderitem b on a.o_id = b.o_id ");
		List<Object> param = new ArrayList<Object>();

		if (f_id > 0) {
			where.append(" where b.f_id = ? ");
			param.add(f_id);
		}
		if(status != null) {
			where.append(" and a.status=?");
			param.add(Integer.parseInt(status));
		}
		if (o_id != null && !"".equals(o_id.trim())) {
			where.append(" and a.o_id like ?");
			param.add("%" + o_id + "%");
		}
		if (createtime != null && !"".equals(createtime.trim())) {
			where.append(" and b.createtime >= ?");
			param.add(createtime);
		}
		
		where.append(" order by a.createTime desc ");
		return paginate(pageNumber, pageSize, "select distinct a.o_id, custerName, custerAddress, createTime", where.toString(),
				param.toArray());
	}

}
